//
//  NSDate+String.m
//  MapDemo
//
//  Created by Fidetro on 16/6/28.
//  Copyright © 2016年 Fidetro. All rights reserved.
//

#import "NSDate+FIDString.h"

@implementation NSDate (FIDString)

+ (NSDate *)getDateWithString:(NSString *)string stringDateFormatter:(NSString *)formatterStr{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:formatterStr];
    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    
    NSDate *date=[formatter dateFromString:string];
    return date;
}

+ (NSDate *)getDateWithStringSince1970ToSecond:(NSString *)string{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[string doubleValue]];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setTimeZone:[NSTimeZone localTimeZone]];

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    return date;
}

+ (NSDate *)getDateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:day];
    [comps setMonth:month];
    [comps setYear:year];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [gregorian setTimeZone:[NSTimeZone localTimeZone]];
    NSDate *date = [gregorian dateFromComponents:comps];
    
    return date;
}

- (NSDate *)beginningWithYear:(NSInteger)year{
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    
    [dateComponentsForDate setYear:year];
    
    NSDate *dateFromDateComponentsForDate = [greCalendar dateFromComponents:dateComponentsForDate];
    
    return dateFromDateComponentsForDate;
}
+ (NSArray<NSDate *>*)getDateWithWeek:(NSInteger)week beginningWithYear:(NSInteger)year{
    NSDate *beginYearDate = [[NSDate date]beginningWithYear:year];
    NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:beginYearDate.timeIntervalSince1970 +week * 6 *86400];
    NSDate *beginDate = [NSDate dateWithTimeIntervalSince1970:endDate.timeIntervalSince1970 - 6 *86400];
    return @[beginDate,endDate];
    
}
+ (NSInteger)ageWithDateOfBirth:(NSDate *)date;
{
    // 出生日期转换 年月日
    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
    NSInteger brithDateYear  = [components1 year];
    NSInteger brithDateDay   = [components1 day];
    NSInteger brithDateMonth = [components1 month];
    
    // 获取系统当前 年月日
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger currentDateYear  = [components2 year];
    NSInteger currentDateDay   = [components2 day];
    NSInteger currentDateMonth = [components2 month];
    
    // 计算年龄
    NSInteger iAge = currentDateYear - brithDateYear - 1;
    if ((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {
        iAge++;
    }
    
    return iAge;
}


+ (NSInteger)ageWithString:(NSString *)string stringDateFormatter:(NSString *)formatterStr;
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:formatterStr];
    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    
    NSDate *date=[formatter dateFromString:string];
    
    // 出生日期转换 年月日
    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
    NSInteger brithDateYear  = [components1 year];
    NSInteger brithDateDay   = [components1 day];
    NSInteger brithDateMonth = [components1 month];
    
    // 获取系统当前 年月日
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger currentDateYear  = [components2 year];
    NSInteger currentDateDay   = [components2 day];
    NSInteger currentDateMonth = [components2 month];
    
    // 计算年龄
    NSInteger iAge = currentDateYear - brithDateYear - 1;
    if ((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {
        iAge++;
    }
    
    return iAge;
}

+ (NSString *)dateStringWithAge:(NSString *)age {
    
    // 获取系统当前 年月日
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger currentDateYear  = [components2 year];
    NSInteger currentDateDay   = [components2 day];
    NSInteger currentDateMonth = [components2 month];
    NSString *month = [NSString string];
    NSString *day = [NSString string];

    if (currentDateMonth < 10) {
        month = [NSString stringWithFormat:@"0%li",(long)currentDateMonth];
     } else {
        month = [NSString stringWithFormat:@"%li",(long)currentDateMonth];
     }
    
    if (currentDateDay < 10) {
        day = [NSString stringWithFormat:@"0%li",(long)currentDateDay];
    } else {
        day = [NSString stringWithFormat:@"%li",(long)currentDateDay];
    }
    
    NSString *string = [NSString stringWithFormat:@"%li-%@-%@",currentDateYear-[age integerValue],month,day];
//    NSLog(@"age:%@",string);

    return string;
}

+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}

@end
